package model

import "gorm.io/gorm"

const (
	UNKNOWN = iota
	HTTP
	SOCKS5
	CLASH
)

type Proxy struct {
	Model   `yaml:"-"`
	Server  string `yaml:"server" gorm:"index;size:128"`
	Port    int    `yaml:"port" gorm:"index"`
	Country string `gorm:"size:64" yaml:"country"`
	City    string `gorm:"size:128"`
	CanUse  int
	Flag    int32
}

func (p *Proxy) IsFlagOn(flag int32) bool {
	if p == nil {
		return false
	}
	return p.Flag&flag != 0
}

func (p *Proxy) SetFlag(flag int32) {
	if p != nil {
		p.Flag |= flag
	}
}

type ProxyHistory struct {
	ProxyInfo
	Speed   uint16
	Success bool
	Google  bool
	Error   string `gorm:"size:128"`
}

type ProxyInfo struct {
	Model
	Type   int
	Target uint `gorm:"index"`
}

func (p *ProxyHistory) BeforeSave(db *gorm.DB) error {
	if len(p.Error) > 128 {
		p.Error = p.Error[:128]
	}
	return nil
}
